<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;

class AdminCompany extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_company';


    protected static function init()
    {
        //TODO:初始化内容
    }
    /**
     * @param $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 根据用户获取公司
     */
    public function companyInfo($uid,$uniacid){
        $dis['b.fans_id'] = $uid;
        $data = Db::name($this->name)
            ->alias('a')
            ->leftJoin('longbing_card_user_info b' ,'a.id = b.company_id')
            ->where($dis)
            ->field('a.*','b.fans_id')
            ->find();
        if(empty($data)){
            $data = Db::name($this->name)->where(['uniacid'=>$uniacid])->find();
        }
        return $data;
    }


    /**
     * @param $dis
     *
     * 获取公司
     */

    public function getCompany($dis){
        $data = $this->where($dis)->find();
        return !empty($data)?$data->toArray():'';
    }


    /**
     *
     * 获取所有公司
     */

    public function companySelect($dis=array()){
        $dis[] = ['status','>',-1];
        $data = $this->where($dis)->select()->toArray();
        return $this->getTree($data,0);
    }



    /**
     *
     * 获取所有公司
     */

    public function companySelectList($dis=array()){

        $data = $this->where($dis)->select()->toArray();

        return $data;
    }

    /**
     * @param $data
     * @param $pId
     * @return array
     * 递归无限极
     */
    public function getTree($data, $pId){
        $tree = array();
        if(!empty($data)){
            foreach($data as $k => $v) {
                if($v['pid'] == $pId) {
                    $v['children'] = $this->getTree($data, $v['id']);
                    if(empty($v['children'])){
                        unset($v['children']);
                    }
                    $tree[]   = $v;
                }
            }
        }
        return $tree;
    }


    /**
     * User: chenniang
     * Date: 2019-09-29 14:10
     * @return void
     * descption:递归获取上级
     */
    public function companyTop($id,&$data=[],$l=0){
       $l++;
        $company = $this->where(['id'=>$id])->field('id,pid')->find();
        if(!empty($company)&&$l<20){
            array_unshift($data,$company['id']);
            $this->companyTop($company['pid'],$data,$l);
        }
        return $data;
    }

    /**
     **@param $id
     * @param array $data
     * @param int $l
     * @功能说明: 递归获取下级
     * @author lichuanming
     * @DataTime: 2020/5/12 13:06
     */
    public function companyLow($id,&$data=[]){
        $company = $this->where(['pid'=>$id])->where('status','>',-1)->field('id,pid')->select()->toArray();
        foreach ($company as  $item){
            if(!empty($item)){
                array_unshift($data,$item['id']);
                $this->companyLow($item['id'],$data);
            }
        }

        return $data;
    }





}